<?php

if (!defined("BASEPATH"))
    exit("No direct script access allowed");
class skill_categories extends MX_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->lang->load('skill_categories');
        $this->load->model("skill_categories_model");
    }

    public function sort($field = "created", $order = "desc")
    {
        $this->skill_categories_model->dataSort($field, $order);
        $this->index();
    }

    public function index($target = 0)
    {
        $arr = $this->skill_categories_model->dataIndex($target);
        $breakCrum = '<a href="' . base_url() . '">' . t("Home") . '</a> » <span>' . t("skill_categories") . '</span>';
        $data = array("content" => $this->load->view("skill_categories/main", $arr, true), "breakCrum" => $breakCrum);
        $this->load->view("templates/admin", $data);
    }

    public function find()
    {
        if (!isset($_POST["txt_find"]))
            return redirect(site_url("skill_categories/index"));
        if (isset($_POST["btnReset"]) && $_POST["btnReset"] == "Reset")
            $this->session->unset_userdata("sm_find_skill_categories");
        else {
            $keyword = $this->input->post("txt_find", true);
            if ($keyword != "") {
                notify_set_messages(notify_info(t("Result") . " " . $keyword), "skill_categories");
                $this->session->set_userdata("sm_find_skill_categories", $keyword);
            }
        }

        redirect(site_url("skill_categories/index"));
    }
    public function filterskill_menus()
    {
        $id = $_POST["mid"];
        if (isset($_POST["btnReset"]))
            $this->session->unset_userdata("sm_filter_skill_menus");
        else
            $this->session->set_userdata("sm_filter_skill_menus", $id);
        redirect(site_url("skill_categories"));
    }
    public function filterskill_categories_top()
    {
        $id = $_POST["top"];
        if (isset($_POST["btnReset"]))
            $this->session->unset_userdata("sm_filter_skill_categories_top");
        else
            $this->session->set_userdata("sm_filter_skill_categories_top", $id);
        
        redirect(site_url("skill_categories"));
    }
    public function add()
    {        
        $arr = $this->skill_categories_model->dataAdd();
        $breakCrum = '<a href="' . base_url() . '">' . t("Home") . '</a> » 
        <a href="/admin/skill_categories">' . t("skill_categories") . '</a> » <span>'.t('Add').'</span>';
        $style = array('js/themes/base/jquery.ui.all.css');
        $script = array('ui/jquery.ui.datepicker.js');
        $data = array(
            "content" => $this->load->view("skill_categories/add", $arr, true),
            "breakCrum" => $breakCrum,
            //'script' => $script,
            //'style' => $style,
            );
        $this->load->view("templates/admin", $data);
    }

    public function edit($id)
    {
        $arr = $this->skill_categories_model->dataEdit($id);
        $breakCrum = '<a href="' . base_url() . '">' . t("Home") . '</a> » 
        <a href="/admin/skill_categories">' . t("skill_categories") . '</a> » <span>'.t('Edit').'</span>';
        $script = array('ui/jquery.ui.datepicker.js');
        $style = array('js/themes/base/jquery.ui.all.css');
        $data = array(
            "content" => $this->load->view("skill_categories/edit", $arr, true),
            "breakCrum" => $breakCrum,
            //'script' => $script,
            //'style' => $style,
            );
        $this->load->view("templates/admin", $data);
    }

    public function doAdd()
    {

        if (!$this->skill_categories_model->checkForm()) {
            notify_set_messages(notify_error(validation_errors()), "skill_categories");
            return $this->add();
        }
        if ($this->skill_categories_model->doUpdate()) {
            notify_set_messages(notify_success(t("Record is added")), "skill_categories");
            return redirect(site_url("skill_categories"));
        }

    }

    public function doEdit()
    {
        if (!$this->skill_categories_model->checkForm()) {
            notify_set_messages(notify_error(validation_errors()), "skill_categories");
            return $this->edit(decrypt($_POST["idx"]));
        }
        if ($this->skill_categories_model->doUpdate()) {
            notify_set_messages(notify_success(t("Record is updated")), "skill_categories");
            redirect(site_url("skill_categories"));
        }

    }

    public function del($id)
    {
        if ($this->skill_categories_model->checkVideoInCategory($id)) {
            notify_set_messages(notify_error('Tồn tại video trong chuyên đề, bạn không được phép xoá chuyên đề!'), "skill_categories");
        } else {
            $result = $this->skill_categories_model->del($id);
            if (is_array($result) && $result[0] == -1) {
                notify_set_messages(notify_atten($result[1]), "skill_categories");
            } else {
                notify_set_messages(notify_success(t("Record is deleted")), "skill_categories");
            }
        }
        
        redirect(site_url("skill_categories"));
    }

}
